/**
 * @(#)BuyerController.java, 2018/2/12.
 * <p/>
 * Copyright 2018 Kerr Wang, Inc. All rights reserved.
 * KERRWANG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package org.wangzx.exercise.controller;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.wangzx.exercise.enums.UserType;
import org.wangzx.exercise.meta.vo.AccountVO;
import org.wangzx.exercise.meta.vo.CartVO;
import org.wangzx.exercise.meta.vo.UserVO;
import org.wangzx.exercise.service.basic.OrderCartService;
import org.wangzx.exercise.service.core.UserCoreService;
import org.wangzx.exercise.utils.LoginUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author hzwangzuxiang(ahscwzx@163.com)
 */
@Controller
@RequestMapping("/")
public class BuyerController extends BaseController{
    static private final Logger logger = LoggerFactory.getLogger(BuyerController.class);

    @Autowired
    private OrderCartService orderCartService;

    @Autowired
    private UserCoreService userCoreService;

    @RequestMapping("/account")
    public ModelAndView account(HttpServletRequest request) {
        ModelAndView mav = genModelAndView("/tmpl/account");
        UserVO userVO = LoginUtils.loginCheck(request);

        logger.info("[op:account] userVO={}", JSON.toJSONString(userVO));
        if(userVO.getLoginType() != UserType.BUYER.getValue()) {
            return genModelAndView("/tmpl/login");
        }

        long userId = userCoreService.getByName(userVO.getUserName()).getId();
        List<AccountVO> list = orderCartService.getAccountList(userId);

        logger.info("[op:account] list={}", JSON.toJSONString(list));

        mav.addObject("user", userVO);
        mav.addObject("data", list);

        return mav;
    }

    @RequestMapping("/itemCart")
    public ModelAndView itemCart(HttpServletRequest request) {
        ModelAndView mav = genModelAndView("/tmpl/itemCart");

        UserVO userVO = LoginUtils.loginCheck(request);
        if(userVO.getLoginType() != UserType.BUYER.getValue()) {
            return genModelAndView("/tmpl/login");
        }

        long userId = userCoreService.getByName(userVO.getUserName()).getId();

        List<CartVO> voList = orderCartService.getCart(userId);

        mav.addObject("user", userVO);
        mav.addObject("data", voList);
        return mav;
    }
}